<?php
namespace  Wcs\SrcManage;

use Wcs;
use Wcs\Config;
//require_once("utils.php");
//require_once("config.inc.php");
require_once('./src/Wcs/auth.php');


class FileManager
{

    /**
     * 删除文件
     * @param $bucketName
     * @param $fileKey
     * @return mixed
     */
    public function delete($bucketName, $fileKey)
    {
        $entry = $bucketName . ':' . $fileKey;
        $encodedEntry = \Wcs\url_safe_base64_encode($entry);

        global $WCS_MGR_URL;

        $url = $WCS_MGR_URL . '/fileManageCmd/delete/' . $encodedEntry;

        $token = \Wcs\get_file_delete_token($bucketName, $fileKey);

        return $this->_do($url, $token);

    }


    /**
     * 获取文件信息
     * @param $bucketName
     * @param $fileKey
     * @return mixed
     */
    public function stat($bucketName, $fileKey)
    {
        $entry = $bucketName . ':' . $fileKey;
        $encodedEntry = \Wcs\url_safe_base64_encode($entry);


        $url = Config::WCS_MGR_URL . '/fileManageCmd/stat/' . $encodedEntry;

        $token = \Wcs\get_file_stat_token($bucketName, $fileKey);

        return $this->_do($url, $token);
    }

    /**
     * 获取公开图片的基本信息
     * @param $bucketName
     * @param $fileName
     * @return string
     */
    public function info_public($bucketName, $fileName)
    {
        return $this->info($bucketName, $fileName, true);
    }

    /**
     * 获取图片的基本信息
     * @param $bucketName
     * @param $fileName
     * @param $isPublic
     * @return string
     */
    public function info($bucketName, $fileName, $isPublic)
    {
        if ($isPublic) {
            $baseUrl = Wcs\WcsFile::getPublicDownloadUrl($bucketName, $fileName) . '?';
        } else {
            $baseUrl = build_private_url($bucketName, $fileName) . '&';
        }
        $url = $baseUrl . 'op=imageInfo';
        dd($url);

        return \Wcs\http_get($url, null);
    }

    /**
     * 执行给定的操作
     * @param $url
     * @param $token
     * @return mixed
     */
    private function _do($url, $token)
    {
        $headers = array("Authorization:$token");

        $resp = \Wcs\http_get($url, $headers);
        return $resp;
    }

} 